package top.itaso.app.ui.login;

import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import top.itaso.app.R;
import top.itaso.app.databinding.FragmentRegisterBinding;
import top.itaso.lib.alog.ALog;
import top.itaso.lib.common.ToastUtils;
import top.itaso.lib.widget.tpv.TpvSettingsTitleBarView;

/**
 * @Description: 用户注册界面
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Gitee https://gitee.com/itsdf07
 * @Date 2022/2/17
 */
public class RegisterFragment extends Fragment {
    private static final String TAG = "RegisterFragment";
    public final String FRAGMENT_TAG = "FRAGMENT_TAG_REGISTER";

    public interface FragmentListener {
        void exitFragment(String fragmentTag);
    }

    private LoginViewModel mViewModel;
    private FragmentRegisterBinding binding;

    private FragmentListener mFragmentListener;

    public static RegisterFragment newInstance() {
        return new RegisterFragment();
    }

    public void setFragmentListener(FragmentListener mFragmentListener) {
        this.mFragmentListener = mFragmentListener;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel = new ViewModelProvider(getActivity()).get(LoginViewModel.class);
        binding = FragmentRegisterBinding.inflate(inflater, container, false);
        View rootView = binding.getRoot();

        TpvSettingsTitleBarView titleBarView = rootView.findViewById(R.id.layout_titlebar);
        titleBarView.setTitleContent("注册");
        titleBarView.setOnBackCallback(new TpvSettingsTitleBarView.IOnBackCallback() {
            @Override
            public void onBack() {
                getActivity().onBackPressed();
            }
        });
        titleBarView.setOnPrivateMarkCallback(new TpvSettingsTitleBarView.IOnPrivateMarkCallback() {
            @Override
            public void onPrivateMarkOpen() {
                ToastUtils.getInstance().showLongToast(getContext(), "暗门事件");
            }
        });

        binding.ivHidePassword.setSelected(false);
        ((LoginActivity) requireActivity()).togglePassword(binding.etInputPassword, binding.ivHidePassword, binding.ivHidePassword.isSelected());
        binding.ivHidePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                ((LoginActivity) requireActivity()).togglePassword(binding.etInputPassword, binding.ivHidePassword, v.isSelected());
            }
        });

        binding.ivHideConfirmPassword.setSelected(false);
        ((LoginActivity) requireActivity()).togglePassword(binding.etInputConfirmPassword, binding.ivHideConfirmPassword, binding.ivHideConfirmPassword.isSelected());
        binding.ivHideConfirmPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                ((LoginActivity) requireActivity()).togglePassword(binding.etInputConfirmPassword, binding.ivHideConfirmPassword, v.isSelected());
            }
        });

        binding.tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(binding.etInputUsername.getText().toString())) {
                    Toast.makeText(requireContext(), "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                String confirmPassword = binding.etInputConfirmPassword.getText().toString();
                if (TextUtils.isEmpty(confirmPassword) || !confirmPassword.equals(binding.etInputPassword.getText().toString())) {
                    Toast.makeText(requireContext(), "密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });

        final String privacyContentTip = getString(R.string.tip_privacy);
        ALog.vTag(TAG, "privacyContentTip:%s", privacyContentTip);
        SpannableString privacySpannable = new SpannableString(privacyContentTip);
        //用户协议 事件、色值处理
        ClickableSpan clickableSpanPrivacy1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://www.makecode.cn/agreement.html");
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //自定义文本颜色
//                ds.setColor(ds.linkColor);
                ds.setColor(getResources().getColor(R.color.color_4169E1));
                //去除文本下划线
                ds.setUnderlineText(false);
                ds.setTypeface(Typeface.DEFAULT_BOLD);
            }
        };
        //隐私协议 事件、色值处理
        ClickableSpan clickableSpanPrivacy2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://www.makecode.cn/privacy.html");
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                //自定义文本颜色
//                ds.setColor(ds.linkColor);
                ds.setColor(getResources().getColor(R.color.color_4169E1));
                ds.setTypeface(Typeface.DEFAULT_BOLD);
                //去除文本下划线M
                ds.setUnderlineText(false);
            }
        };
        //设置高亮的起始、结束位置
        privacySpannable.setSpan(clickableSpanPrivacy1, privacyContentTip.length() - 9, privacyContentTip.length() - 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        privacySpannable.setSpan(clickableSpanPrivacy2, privacyContentTip.length() - 4, privacyContentTip.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        binding.tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance());// 需设置了才能有点击功能
        binding.tvPrivacy.setText(privacySpannable);

        final String websitUrl = getString(R.string.nav_header_subtitle, "gitee.com/itsdf07");
        SpannableString spanWebsitUrl = new SpannableString(websitUrl);
        URLSpan urlSpanWebsit = new URLSpan("https://gitee.com/itsdf07");//设置跳转的 url 地址
        spanWebsitUrl.setSpan(urlSpanWebsit, 5, websitUrl.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        binding.tvWebsit.setMovementMethod(LinkMovementMethod.getInstance());
        binding.tvWebsit.setText(spanWebsitUrl);
        return rootView;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

}